/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 * 2014-04-27     Bernard      make code cleanup. 
 */

#include <board.h>
#include <rtthread.h>

#ifdef RT_USING_LWIP
#include <lwip/sys.h>
#include <lwip/api.h>
#include <netif/ethernetif.h>
#include "stm32f4xx_eth.h"
#endif

#ifdef RT_USING_FINSH
#include <shell.h>
#include <finsh.h>
#endif

#ifdef RT_USING_GDB
#include <gdb_stub.h>
#endif

void rt_init_thread_entry(void *parameter)
{

	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOC, ENABLE); //使能GPIOF时钟

	//GPIOF9,F10初始化设置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		   //LED0和LED1对应IO口 F9  F10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;	  //普通输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	 //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;   // 悬空
	GPIO_Init(GPIOF, &GPIO_InitStructure);			   //初始化GPIOF

	// GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOF

	GPIO_SetBits(GPIOF, GPIO_Pin_9); //GPIOF9,F10设置高，灯灭

	/* GDB STUB */
#ifdef RT_USING_GDB
	gdb_set_device("uart6");
	gdb_start();
#endif

	/* LwIP Initialization */
#ifdef RT_USING_LWIP
	{
		extern void lwip_sys_init(void);

		/* register ethernetif device */
		eth_system_device_init();

		rt_hw_stm32_eth_init();

		/* init lwip system */
		lwip_sys_init();
		rt_kprintf("TCP/IP initialized!\n");
	}
#endif

#ifdef RT_USING_FINSH
	/* init finsh */
	finsh_system_init();
#endif

	while (1)
	{
		GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
		rt_thread_delay(50);
	}
}
#include "motor.h"
void user_motor_thread_entry(void *parameter)
{
	user_device_pwm_init();
	while (1)
	{
		//			TIM_SetCompare1(MOTOR_TIMx, 1000);  //8
		//      TIM_SetCompare2(MOTOR_TIMx, 0);// 9
		//			TIM_SetCompare3(MOTOR_TIMx, 0);//10
		//      TIM_SetCompare4(MOTOR_TIMx, 1000);//11

		//		user_device_pwm_duty_set(MOTOR_LEFT_SELECT,
		//																						MOTOR_DIR_SELECT_BACKWARD,
		//																									1000);
		//		user_device_pwm_duty_set(MOTOR_RIGHT_SELECT,
		//																						MOTOR_DIR_SELECT_BACKWARD,
		//																						1000);
		rt_thread_delay(10);
	}
}

#include "Ultrasonic.h"
uint32_t aaa = 0;
void user_ultrasonic_thread_entry(void *parameter)
{

	ultrasonic_init();
	while (1)
	{
		rt_thread_delay(10);
	}
}


#include "blueteeth.h"
void user_blue_teeth_thread_entry(void *parameter)
{
	BlueTeeth_Init();
	while (1)
	{
		HandCtrl();
	}
}
int rt_application_init()
{
	rt_thread_t tid;
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;			  //TIM3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;		  //从优先级4级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			  //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);							  //初始化NVIC寄存器

	tid = rt_thread_create("init",
						   rt_init_thread_entry, RT_NULL,
						   2048, RT_THREAD_PRIORITY_MAX / 3, 20);

	if (tid != RT_NULL)
		rt_thread_startup(tid);

	tid = rt_thread_create("user_motor",
						   user_motor_thread_entry, RT_NULL,
						   2048, RT_THREAD_PRIORITY_MAX / 3, 20);
	if (tid != RT_NULL)
		rt_thread_startup(tid);

	tid = rt_thread_create("user_blueteeth",
						   user_blue_teeth_thread_entry, RT_NULL,
						   2048, RT_THREAD_PRIORITY_MAX / 3, 20);
	if (tid != RT_NULL)
		rt_thread_startup(tid);

	tid = rt_thread_create("user_ultrasonic",
						   user_ultrasonic_thread_entry, RT_NULL,
						   2048, RT_THREAD_PRIORITY_MAX / 3, 20);
	if (tid != RT_NULL)
		rt_thread_startup(tid);

	return 0;
}
